/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import com.mysql.jdbc.CallableStatement;
import dbhelper.dbconnect;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import POJO.cuaHangPOJO;
/**
 *
 * @author SoftVnn Member
 */
public class cuaHangDAO {
    public ArrayList<cuaHangPOJO> getAll()
    {
        ArrayList<cuaHangPOJO> arr = new ArrayList<cuaHangPOJO>();
        try
        {
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spCuaHang_getAll();");
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                cuaHangPOJO obj = new cuaHangPOJO();
                obj.setMaCuaHang(rs.getString("MaCuaHang"));
                obj.setMaKhuVuc(rs.getString("MaKhuVuc"));
                obj.setTenCuaHang(rs.getString("TenCuaHang"));
                obj.setDiaChi(rs.getString("DiaChi"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error;
        }
        return arr;
    }
    public ArrayList<cuaHangPOJO> get(String MaCuaHang)
    {
        ArrayList<cuaHangPOJO> arr = new ArrayList<cuaHangPOJO>();
        try
        {           
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spCuaHang_get('%s');", MaCuaHang);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                cuaHangPOJO obj = new cuaHangPOJO();
                obj.setMaCuaHang(rs.getString("MaCuaHang"));
                obj.setMaKhuVuc(rs.getString("MaKhuVuc"));
                obj.setTenCuaHang(rs.getString("TenCuaHang"));
                obj.setDiaChi(rs.getString("DiaChi"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return arr;
    }
    public int insert(String MaCuaHang, String MaKhuVuc, String TenCuaHang,String DiaChi)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spCuaHang_insert('%s','%s','%s','%s');", MaCuaHang,MaKhuVuc, TenCuaHang,DiaChi);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public int update(String MaCuaHang, String MaKhuVuc, String TenCuaHang,String DiaChi)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spCuaHang_update('%s','%s','%s','%s');", MaCuaHang,MaKhuVuc, TenCuaHang,DiaChi);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public int delete(String MaCuaHang)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spCuaHang_delete('%s');", MaCuaHang);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public String  issue()
    {
        String MaCuaHangMoi = "null";
        try
        {
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spCuaHang_issue();");
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {                
                MaCuaHangMoi = rs.getString("MaCuaHangMoi");
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error;
        }
        return MaCuaHangMoi;
    }
}
